package com.linln.modules.withdrawal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linln.common.enums.WithdrawalEnum;
import com.linln.common.utils.DateUtil;
import com.linln.common.utils.PathMappingUtil;
import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import com.linln.modules.capital.domain.ShopCapital;
import com.linln.modules.capital.service.ShopCapitalService;
import org.elasticsearch.common.io.PathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.modules.withdrawal.domain.Withdrawal;
import com.linln.modules.withdrawal.repository.WithdrawalMapper;
import com.linln.modules.withdrawal.service.WithdrawalService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.modules.withdrawal.service.impl
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.modules.withdrawal.service.impl
 * @Description: TODO
 * @date Date : 2020年12月30日 10:50
 */

@Service
public class WithdrawalServiceImpl extends ServiceImpl<WithdrawalMapper, Withdrawal> implements WithdrawalService {

    @Autowired
    private ShopCapitalService shopCapitalService;

    @Autowired
    private WithdrawalMapper withdrawalMapper;

    //申请提现
    @Override
    @Transactional
    public JsonResult applyWithdrawal(String shopId, Double money) {
        ShopCapital shopCapitalByShopId = shopCapitalService.getShopCapitalByShopId(shopId);
        if (shopCapitalByShopId.getAvailableAmount() < money) {
            return JsonResult.error("申请失败，余额不足");
        }
        shopCapitalByShopId.setFrozenAmount(shopCapitalByShopId.getFrozenAmount() + money);
        shopCapitalByShopId.setAvailableAmount(shopCapitalByShopId.getAvailableAmount() - money);
        shopCapitalService.updateShopCapital(shopCapitalByShopId);
        Withdrawal withdrawal = new Withdrawal();
        withdrawal.setShopId(shopId);
        withdrawal.setWithdrawalMoney(money);
        withdrawal.setWithdrawalDate(DateUtil.formatDate(new Date()));
        //状态
        withdrawal.setStatus(WithdrawalEnum.WAITEXAMINE.getCode());
        if (this.save(withdrawal)) {
            return JsonResult.ok();
        }

        return JsonResult.error();
    }

    @Override
    public Map<String, Object> getWithdrawal(Integer pageNum, Integer limit, String shopId, Integer status) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum + 1, limit);
        PageInfo<Withdrawal> pageInfo = new PageInfo<Withdrawal>(withdrawalMapper.getWithdrawalList(shopId, status));
        map.put("code", 200);
        map.put("data", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    @Override
    public PageResult<Withdrawal> getLayuiWithdrawal(Integer pageNum, Integer limit, String shopName, String phone, Integer status) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum, limit);
        PageInfo<Withdrawal> pageInfo = new PageInfo<Withdrawal>(withdrawalMapper.getWithdrawalInfoList(shopName, phone, status));
        PageResult<Withdrawal> pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setCount(pageInfo.getTotal());
        pageResult.setCode(0);
        return pageResult;
    }

    @Override
    public Withdrawal getWithdrawalInfo(String withdrawalId) {
        return withdrawalMapper.getWithdrawalInfo(withdrawalId);
    }

    //跟新审核状态
    @Override
    @Transactional
    public boolean updateExamineStatus(String withdrawalId, Integer status, String refuseReason, long userId) {
        if (WithdrawalEnum.REFUSEEXAMINE.getCode().equals(status)) {
            Withdrawal withdrawal = this.getById(withdrawalId);
            ShopCapital shopCapitalByShopId = shopCapitalService.getShopCapitalByShopId(withdrawal.getShopId());
            shopCapitalByShopId.setFrozenAmount(shopCapitalByShopId.getFrozenAmount() - withdrawal.getWithdrawalMoney());
            shopCapitalByShopId.setAvailableAmount(shopCapitalByShopId.getAvailableAmount() + withdrawal.getWithdrawalMoney());
            if (!shopCapitalService.updateShopCapital(shopCapitalByShopId)) {
                throw new RuntimeException("更新账单失败");
            }
        }
        return this.updateById(new Withdrawal(withdrawalId, userId, status, refuseReason, DateUtil.formatDate(new Date())));
    }

    //已转账
    @Override
    public boolean adoptTransfer(String withdrawalId, MultipartFile file) {
        String path = PathMappingUtil.saveImg(PathMappingUtil.IMGPATH, PathMappingUtil.IMGPATHMAPPING, file);
        Withdrawal withdrawal = new Withdrawal(withdrawalId, WithdrawalEnum.ADOPTTRANSFER.getCode(), path, DateUtil.formatDate(new Date()));
        return this.updateById(withdrawal);
    }
}
